/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = t_wtc_detect.c                                    */
/*  NOTE      =                                                    */
/*  DATE      = 2016/02/24 by songbb                               */
/*******************************************************************/

#include <CUnit/Basic.h>
#include <stdlib.h>

#include "frcomm.h"
#include "frmem.h"

#include "../../../src/module/tuxedowtc/config.h"
#include "../test_wtc_comm.h"
#include <tuxedowtc/wtc_core.h>
#include <tuxedowtc/wtc_header.h>
#include <tuxedowtc/wtc_comm.h>
#include <tuxedowtc/wtc_packet.h>
#include <frct_comm.h>
#include <apmtest_comm.h>

static int __suite_init(void)
{
    return 0;
}

static int __suite_cleanup(void)
{
    return 0;
}

/**
 * 正常测试
 */
static void __case1(void)
{
    Conntrack *p_st_conn = NULL;
    CU_ASSERT_EQUAL(test_wtc_create_ct(&p_st_conn, TEST_WTC_SRC_IP, TEST_WTC_DST_IP, TEST_WTC_SRC_PORT, TEST_WTC_DST_PORT), FR_SUCC);

    char *p_bin = (char *) "739038420000000e00000001ffffffff0000000000000000000000010000021c0000005700000054000000430000004c0000006f00000063000000610000006c000000410000005000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004800000014000000010001001b";
    char arr_payload[65536] = {'\0'};

    CU_ASSERT_EQUAL(apmtest_hex_string_to_bin(p_bin, arr_payload, sizeof(arr_payload)), FR_SUCC);

    unsigned int ui_payload_len = strlen(p_bin) / 2;
    Apm_packet *p_st_packet = NULL;
    struct timeval st_timestamp;
    st_timestamp.tv_sec = 1454549354L;
    st_timestamp.tv_usec = 123456L;
    CU_ASSERT_EQUAL(apmtest_create_apm_packet(&p_st_packet, arr_payload, ui_payload_len, TEST_WTC_SRC_IP, TEST_WTC_DST_IP, TEST_WTC_SRC_PORT, TEST_WTC_DST_PORT, st_timestamp), FR_SUCC);

    CU_ASSERT_EQUAL(wtc_detect(p_st_conn, p_st_packet), APM_L7_IS_YES);

    apmtest_destroy_apm_packet(&p_st_packet);
    CU_ASSERT_PTR_NULL(p_st_packet);
    apmtest_destroy_ct(&p_st_conn);
    CU_ASSERT_PTR_NULL(p_st_conn);
}

/**
 * 正常测试
 */
static void __case2(void)
{
    Conntrack *p_st_conn = NULL;
    CU_ASSERT_EQUAL(test_wtc_create_ct(&p_st_conn, TEST_WTC_SRC_IP, TEST_WTC_DST_IP, TEST_WTC_SRC_PORT, TEST_WTC_DST_PORT), FR_SUCC);

    char *p_bin = (char *) "000000000000001400000000fffffffe000001b0800000010000000000000000910398580000001100010000000000000000002c0000123000000006535452494e47000000000000000000096c6f77657263617365000000001300000000005400001230ffffd8f0000000487fffffff00000003000000000000001003c9cee40000bea10bc9ced50000000900000001000000000dc9cee5000000120000000a00006c5e1fa3e5e6703a00000007000000000058000012300000000100000002ffffffff0000000000000000000000000000000000000000000000000000000000000000000000320000000000000000000000000000000000000007544f555050455200000a0000000000ac000012300000001e00000020000000000000000000000000000000000000006c0000005e0000001fffffffa3ffffffe5ffffffe6000000700000003a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a5754434c6f63616c41500000";
    char arr_payload[65536] = {'\0'};

    CU_ASSERT_EQUAL(apmtest_hex_string_to_bin(p_bin, arr_payload, sizeof(arr_payload)), FR_SUCC);

    unsigned int ui_payload_len = strlen(p_bin) / 2;
    Apm_packet *p_st_packet = NULL;
    struct timeval st_timestamp;
    st_timestamp.tv_sec = 1454549354L;
    st_timestamp.tv_usec = 123456L;
    CU_ASSERT_EQUAL(apmtest_create_apm_packet(&p_st_packet, arr_payload, ui_payload_len, TEST_WTC_SRC_IP, TEST_WTC_DST_IP, TEST_WTC_SRC_PORT, TEST_WTC_DST_PORT, st_timestamp), FR_SUCC);

    CU_ASSERT_EQUAL(wtc_detect(p_st_conn, p_st_packet), APM_L7_IS_YES);

    apmtest_destroy_apm_packet(&p_st_packet);
    CU_ASSERT_PTR_NULL(p_st_packet);
    apmtest_destroy_ct(&p_st_conn);
    CU_ASSERT_PTR_NULL(p_st_conn);
}

/**
 * 异常测试
 * 包长小于通用头长度
 */
static void __case3(void)
{
    Conntrack *p_st_conn = NULL;
    CU_ASSERT_EQUAL(test_wtc_create_ct(&p_st_conn, TEST_WTC_SRC_IP, TEST_WTC_DST_IP, TEST_WTC_SRC_PORT, TEST_WTC_DST_PORT), FR_SUCC);

    char *p_bin = (char *) "739038420000000e00000001ffffffff0000000000000000000000010000";
    char arr_payload[65536] = {'\0'};

    CU_ASSERT_EQUAL(apmtest_hex_string_to_bin(p_bin, arr_payload, sizeof(arr_payload)), FR_SUCC);

    unsigned int ui_payload_len = strlen(p_bin) / 2;
    Apm_packet *p_st_packet = NULL;
    struct timeval st_timestamp;
    st_timestamp.tv_sec = 1454549354L;
    st_timestamp.tv_usec = 123456L;
    CU_ASSERT_EQUAL(apmtest_create_apm_packet(&p_st_packet, arr_payload, ui_payload_len, TEST_WTC_SRC_IP, TEST_WTC_DST_IP, TEST_WTC_SRC_PORT, TEST_WTC_DST_PORT, st_timestamp), FR_SUCC);

    CU_ASSERT_EQUAL(wtc_detect(p_st_conn, p_st_packet), APM_L7_IS_UNKNOWN);

    apmtest_destroy_apm_packet(&p_st_packet);
    CU_ASSERT_PTR_NULL(p_st_packet);
    apmtest_destroy_ct(&p_st_conn);
    CU_ASSERT_PTR_NULL(p_st_conn);
}

int add_suite_wtc_detect(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" wtc_detect()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    if (CU_add_test(pSuite, "case1", __case1) == NULL ||
        CU_add_test(pSuite, "case2", __case2) == NULL ||
        CU_add_test(pSuite, "case3", __case3) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
